package com.meridian.commerce.web;

import com.meridian.commerce.model.UserAccount;
import com.meridian.commerce.model.UserAccountAddress;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.roo.addon.web.mvc.controller.RooConversionService;
        
/**
 * A central place to register application Converters and Formatters. 
 */
@RooConversionService
public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

	@Override
	protected void installFormatters(FormatterRegistry registry) {
		super.installFormatters(registry);
		// Register application converters and formatters
	}
	

	Converter<UserAccount, String> getUserAccountConverter() {
        return new Converter<UserAccount, String>() {
            public String convert(UserAccount useraccount) {
                return new StringBuilder().append(useraccount.getFirstName()).append(" ").append(useraccount.getMiddleName()).append(" ").append(useraccount.getLastName()).append(" ").toString();
            }
        };
    }

	org.springframework.core.convert.converter.Converter<UserAccountAddress, String> getUserAccountAddressConverter() {
        return new org.springframework.core.convert.converter.Converter<UserAccountAddress, String>() {
            public String convert(UserAccountAddress useraccountaddress) {
                return new StringBuilder().append(useraccountaddress.getFullName()).append(" ").append(useraccountaddress.getAddressLine1()).append(" ").append(useraccountaddress.getAddressLine2()).append(" ").append(useraccountaddress.getCity()).toString();
            }
        };
    }

	public void installLabelConverters(FormatterRegistry registry) {
        registry.addConverter(getUserAccountConverter());
        registry.addConverter(getUserAccountAddressConverter());
    }

	public void afterPropertiesSet() {
        super.afterPropertiesSet();
        installLabelConverters(getObject());
    }
}
